package com.xiaoyu.base.entity;

import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;

import com.xiaoyu.base.converter.StringListConverter;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

import in.srain.cube.request.JsonData;

@Entity(tableName = "hot_area")
public class HotAreaEntity {

    @PrimaryKey
    @NonNull
    private String province;
    @TypeConverters({StringListConverter.class})
    private List<String> cities;

    public HotAreaEntity() {
        province = "";
        cities = new ArrayList<>();
    }

    public HotAreaEntity(JsonData jsonData) {
        province = jsonData.optString("province");
        cities = new LinkedList<>();

        List<JsonData> cityDataList = jsonData.optJson("cities").toList();
        for (JsonData cityRawData : cityDataList) {
            String cityName = cityRawData.optString("city");
            cities.add(cityName);
        }
    }

    @NonNull
    public String getProvince() {
        return province;
    }

    public void setProvince(@NonNull String province) {
        this.province = province;
    }

    public List<String> getCities() {
        return cities;
    }

    public void setCities(List<String> cities) {
        this.cities = cities;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof HotAreaEntity)) {
            return false;
        }
        HotAreaEntity other = (HotAreaEntity) o;
        if (!TextUtils.equals(province, other.province)) {
            return false;
        }

        if (cities == null || other.cities == null) {
            return false;
        } else if (cities.size() != other.cities.size()) {
            return false;
        } else {
            for (int i = 0; i < cities.size(); i++) {
                if (!TextUtils.equals(cities.get(i), other.cities.get(i))) {
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        return Objects.hash(province);
    }

    @NonNull
    @Override
    public String toString() {
        return "HotAreaEntity{" +
                "province='" + province + '\'' +
                ", cities=" + cities +
                '}';
    }
}
